<?php
// +----------------------------------------------------------------------
// | 帖子分类
// +----------------------------------------------------------------------

namespace App\Http\Controllers\App\Forum;

use App\Models\ForumCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ForumCategoryController extends Controller
{
    /**
     * @OA\Get(path="/forum_category",tags={"帖子分类"},summary="列表",
     *   @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="int", default="1")),
     *   @OA\Parameter(name="limit", in="query", description="行数", @OA\Schema(type="int", default="10")),
     *   @OA\Response(response="200", description="successful operation")
     * )
     */
    public function index()
    {
        $where = [];
        if (!empty($request['id'])) {
            $where[] = ['id', '=', $request['id']];
        }
        if (!empty($request['name'])) {
            $where[] = ['name', '=', $request['name']];
        }
        $list = ForumCategory::where($where)->order('id desc')->paginate(10);
        return $this->result(0,'', $list->items(), ['count' => $list->total()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * @OA\Post(path="/forum_category",tags={"帖子分类"},summary="新增",
     *   @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
     *   @OA\RequestBody(
     *     @OA\MediaType(
     *       mediaType="content-type/json",
     *         @OA\Schema(
     *           @OA\Property(description="上级分类", property="parent_id", type="integer"),
     *           @OA\Property(description="分类名称", property="title", type="string"),
     *           @OA\Property(description="分类排序", property="sort", type="integer"),
     *           required={"parent_id", "title"})
     *       )
     *     ),
     *   @OA\Response(response="200", description="successful operation")
     * )
     */
    public function store()
    {
        $result  = ForumCategory::create($this->request->param());
        if (!$result) {
            return $this->error('保存失败');
        }
        return $this->success('保存成功');
    }

    /**
     * @OA\Get(path="/forum_category/{id}",tags={"帖子分类"},summary="详情",
     *   @OA\Parameter(name="id", in="path", description="id", @OA\Schema(type="int")),
     *   @OA\Response(response="200", description="successful operation")
     * )
    * @param  \App\Models\ForumCategory  $forumCategory
    * @return \Illuminate\Http\Response
    */
    public function show(ForumCategory $forumCategory)
    {
        //$data = ArticleModel::whereId($id)->find();
        //return $this->jsonReturn(200, 'success', $data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\ForumCategory  $forumCategory
     * @return \Illuminate\Http\Response
     */
    public function edit(ForumCategory $forumCategory)
    {
        //
    }

    /**
     * @OA\Put(path="/forum_category/{id}",tags={"帖子分类"},summary="编辑",
     *   @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
     *   @OA\Parameter(name="id", in="path", description="id", @OA\Schema(type="int")),
     *   @OA\RequestBody(
     *     @OA\MediaType(
     *       mediaType="content-type/json",
     *         @OA\Schema(
     *           @OA\Property(description="上级分类", property="parent_id", type="integer"),
     *           @OA\Property(description="分类名称", property="title", type="string"),
     *           @OA\Property(description="分类排序", property="sort", type="integer"),
     *           required={"parent_id", "title"})
     *       )
     *     ),
     *   @OA\Response(response="200", description="successful operation")
     * )
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\ForumCategory  $forumCategory
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ForumCategory $forumCategory)
    {
        //$data = $this->request->param();
        //ForumCategory::update($data, ['id' => $id]);
        //return $this->success('保存成功');
    }

    /**
     * @OA\Delete(path="/forum_category/{id}",tags={"帖子分类"},summary="删除",
     *   @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
     *   @OA\Parameter(name="id", in="path", description="id", @OA\Schema(type="int")),
     *   @OA\Response(response="200", description="successful operation")
     * )
     * @param  \App\Models\ForumCategory  $forumCategory
     * @return \Illuminate\Http\Response
     */
    public function destroy(ForumCategory $forumCategory)
    {
        $result = $forumCategory->delete();
        if ($result) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
}
